package com.hongxun.budget.ui.activity.tk;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.utils.MyAMapUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class EditeLatLngActivity extends BaseActivity {

    @BindView(R.id.right_save)
    Button rightSave;
    @BindView(R.id.map)
    MapView mMapView;
    AMap aMap;
    Double lat, lgt;

    @Override
    public int getLayoutId() {
        return R.layout.activity_edite_lat_lng;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        Intent intent = getIntent();
        lat = intent.getDoubleExtra("lat", 0);
        lgt = intent.getDoubleExtra("lgt", 0);
        rightSave.setVisibility(View.VISIBLE);
        rightSave.setText("确定");
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lgt), MyAMapUtils.locationZoom));
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
                lat = cameraPosition.target.latitude;
                lgt = cameraPosition.target.longitude;
            }
        });

        aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
            @Override
            public void onMapLoaded() {
                addMarkerInScreenCenter();
            }
        });

    }

    private void addMarkerInScreenCenter() {
        LatLng latLng = aMap.getCameraPosition().target;
        Point screenPosition = aMap.getProjection().toScreenLocation(latLng);
        Marker locationMarker = aMap.addMarker(new MarkerOptions()
                .anchor(0.5f, 0.5f)
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.purple_pin)));
        //设置Marker在屏幕上,不跟随地图移动
        locationMarker.setPositionByPixels(screenPosition.x, screenPosition.y);
        locationMarker.setZIndex(1);

    }

    @OnClick(R.id.right_save)
    public void onViewClicked() {
        Intent intent = new Intent();
        intent.putExtra("lat", lat);
        intent.putExtra("lgt", lgt);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}
